package com.example.exercise;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.exercise.Base.CountBase;
import com.example.exercise.Base.User;
import com.example.exercise.MysqlUtil.DBService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class tongji extends AppCompatActivity {

    ListView ls,ls2;
    Button enter;
    List<User> list1 ;
    List<CountBase> list2;
    private List<Map<String,Object>> li2=new ArrayList<Map<String,Object>>();
    private List<Map<String,Object>> li3=new ArrayList<Map<String,Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tongji);
        ls = findViewById(R.id.renshu);
        enter = findViewById(R.id.button2);
        ls2 = findViewById(R.id.ls2);

        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new  Thread(new Runnable() {
                    @Override
                    public void run() {

                        list1 = DBService.getUserSign();
                        list2 = DBService.getCount();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                User user;

                                for( int i=0;i<list1.size();i++)
                                {
                                    user= list1.get(i);
                                    Map<String,Object> item=new HashMap<String,Object>();
                                    item.put("name", user.getName());
                                    item.put("classroom",user.getClassroom());
                                    li2.add(item);
                                }
                                ls.setAdapter(new SimpleAdapter(getApplicationContext(),
                                        li2,R.layout.list_base,new String[]{"name","classroom"},
                                        new int[]{R.id.textView59,R.id.textView60}));



                                CountBase countBase;

                                for( int i=0;i<list2.size();i++)
                                {
                                    countBase= list2.get(i);
                                    Map<String,Object> item=new HashMap<String,Object>();
                                    item.put("classroom", countBase.getClassroom());
                                    item.put("count",countBase.getCount());
                                    li3.add(item);
                                }
                                ls2.setAdapter(new SimpleAdapter(getApplicationContext(),
                                        li3,R.layout.list_base2,new String[]{"classroom","count"},
                                        new int[]{R.id.textView61,R.id.textView62}));
                            }
                        });
                    }
                }).start();
            }
        });
    }
}